package com.soft.app.ruanba;

import java.io.File;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.soft.util.RuanXmlHelper;

public class RuanXmlToDB implements com.soft.app.ExecutableJob{
	class FileFilter implements java.io.FilenameFilter
	{

		final RuanXmlToDB x;
		
		FileFilter()
		{
			super();
			x = new RuanXmlToDB(dataDirPath);
		}
		
		public boolean accept(java.io.File dir, String name)
		{
			return name.endsWith(".xml");
		}
	}

	private static final Logger log = LoggerFactory.getLogger(RuanXmlToDB.class);
	private String dataDirPath;

	public RuanXmlToDB(String dataDirPath)
	{
		this.dataDirPath = null;
		this.dataDirPath = dataDirPath;
	}

	public void execute()
	{
		java.io.File dataDir = new File(dataDirPath);
		java.io.File files[] = null;
		if (dataDir.isDirectory())
			files = dataDir.listFiles(new FileFilter());
		if (files == null)
		{
			log.error("没有在指定目录下发现数据文件,系统退出!");
			System.exit(-1);
		}
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser saxParser = null;
		try
		{
			saxParser = spf.newSAXParser();
		}
		catch (ParserConfigurationException e)
		{
			log.error("解析器配置错误:{}", e);
		}
		catch (org.xml.sax.SAXException e)
		{
			log.error("解析器初始化错误:{}", e);
		}
		java.io.File arr$[] = files;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++)
		{
			java.io.File f = arr$[i$];
			try
			{
				saxParser.parse(f, new RuanXmlHelper());
				continue;
			}
			catch (Exception ex)
			{
				log.error("转换XML到数据库时出现错误{}", ex);
			}
			System.exit(1);
		}

	}

	public static void main(String ag[])
	{
		RuanXmlToDB j = new RuanXmlToDB("E:\\ruan8\\xml");
		j.execute();
	}
}
